const {
  userscreate,
  roleslis
} = require('../../utils/api')
Page({
  data: {
    nickname: '',
    phone: '',
    username: '',
    password: '',
    roleslis: []
  },
  onShow() {
    this.getRoleslis()
  },
  //获取注册用户的角色
  getRoleslis() {
    const data = {
      pageSize: '200',
      filter: {}
    }
    roleslis(data).then(res => {
      const arr = []
      res.data.forEach(item => {
        if (item.title == '司机端') {
          arr.push(item)
        }
        this.setData({
          roleslis: arr
        })
      })
    })
  },

  onNameChange(e) {
    this.setData({
      nickname: e.detail
    });
  },
  onAccountChange(e) {
    this.setData({
      username: e.detail
    });
  },

  onPhoneChange(e) {
    this.setData({
      phone: e.detail
    });
  },

  onPasswordChange(e) {
    this.setData({
      password: e.detail
    });
  },

  handleSubmit() {
    const {
      nickname,
      phone,
      username,
      password
    } = this.data;

    if (!/^1[3-9]\d{9}$/.test(phone)) {
      wx.showToast({
        title: '手机号格式错误',
        icon: 'none'
      });
      return;
    }

    if (!nickname || !username || !phone || !password) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }
    //注册信息
    const data = {
      nickname: this.data.nickname,
      phone: this.data.phone,
      username: this.data.username,
      password: this.data.password,
      roles: this.data.roleslis
    }
    userscreate(data).then(res => {
      console.log('测试注册用户信息', res)
      JSON.stringify(res.data)
            wx.navigateTo({
              url: `/pages/siji/siji?userInfo=${JSON.stringify(res.data)}&padd=${this.data.password}&user=${this.data.username}`
            });
    })



    // 调用注册接口
    // wx.request({
    //   url: 'https://your-api.com/register',
    //   method: 'POST',
    //   data: {
    //     name,
    //     phone,
    //     password
    //   },
    //   success: (res) => {
    //     if (res.data.code === 200) {
    //       wx.navigateTo({
    //         url: '/pages/login/login'
    //       });
    //     }
    //   }
    // });
  }
});